

package com.maoshi.shop.security.api.config;

import com.maoshi.shop.security.comment.filter.MaoshiAuthenticationProcessingFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.List;

/**
 * @author LGH
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Autowired
    private List<MaoshiAuthenticationProcessingFilter> maoshiAuthenticationProcessingFilters;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        maoshiAuthenticationProcessingFilters.forEach((maoshiAuthenticationProcessingFilter)
                -> http.addFilterBefore(maoshiAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class));

        http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
            .and()
            .requestMatchers().anyRequest()
            .and()
            .anonymous()
            .and()
            .authorizeRequests()
            //配置/p访问控制，必须认证过后才可以访问
            .antMatchers("/p/**").authenticated();
    }


}
